package com.hskn.hss.module.storageDetail.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.costdictitem.entity.CostDictItem;
import com.hskn.hss.module.costdictitem.mapper.CostDictItemMapper;
import com.hskn.hss.module.planDetail.entity.TlPlanDetail;
import com.hskn.hss.module.storageDetail.entity.TlStorageDetail;
import com.hskn.hss.module.storageDetail.mapper.TlStorageDetailMapper;
import com.hskn.hss.module.storageDetail.service.ITlStorageDetailService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-07
 */
@Service
public class TlStorageDetailServiceImpl extends ServiceImpl<TlStorageDetailMapper, TlStorageDetail> implements ITlStorageDetailService {
    @Autowired
    TlStorageDetailMapper tlStorageDetailMapper;
    @Autowired
    CostDictItemMapper costDictItemMapper;


    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        TlStorageDetail tlStorageDetail = params.getObject("data", TlStorageDetail.class);
        tlStorageDetail.setId(StringUtils.uuid());
        CostDictItem costDictItem = costDictItemMapper.selectById(tlStorageDetail.getItemType());
        tlStorageDetail.setItemUnit(costDictItem.getUnit());
        int i = tlStorageDetailMapper.insert(tlStorageDetail);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        TlStorageDetail tlStorageDetail = params.getObject("data", TlStorageDetail.class);
        CostDictItem costDictItem = costDictItemMapper.selectById(tlStorageDetail.getItemType());
        tlStorageDetail.setItemUnit(costDictItem.getUnit());
        int i = tlStorageDetailMapper.updateById(tlStorageDetail);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public String updateadd(String centerid, String calcFunc, List<TlPlanDetail> tlPlanDetailList) throws HssException {
        String msg = "操作成功";
        for (TlPlanDetail plandetail : tlPlanDetailList) {
            String itemClass = plandetail.getItemclass();
            String itemType = plandetail.getItemtype();
            int num = plandetail.getItemnum();
            Map querymap = new HashMap<>();
            querymap.put("storage_id", centerid);
            querymap.put("item_class", itemClass);
            querymap.put("item_type", itemType);
            List<TlStorageDetail> tlStorageDetaillist = tlStorageDetailMapper.selectByMap(querymap);
            TlStorageDetail tlStorageDetail = tlStorageDetaillist.get(0);
            int sumNum = tlStorageDetail.getItemNum();
            if (calcFunc.equals("add")) { //计算方法 add 为入库
                sumNum += num;
            } else {
                sumNum -= num;
                if (sumNum < num) {
                    msg = "库存不足";
                    break;
                }
            }
            tlStorageDetail.setItemNum(sumNum);
            tlStorageDetailMapper.updateById(tlStorageDetail);
        }
        return msg;
    }

    @Override
    public TlStorageDetail getDetailById(String id) throws HssException {
        QueryWrapper<TlStorageDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlStorageDetail::getId, id);
        TlStorageDetail tlStorageDetail = tlStorageDetailMapper.selectOne(queryWrapper);
        return tlStorageDetail;
    }

    @Override
    public AjaxResult deleteByid(String id) throws HssException {
        try {
            tlStorageDetailMapper.deleteById(id);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public List<TlStorageDetail> getList(String storageid) throws HssException {
        Map map = new HashMap();
        map.put("storage_id", storageid);
        List<TlStorageDetail> tlStorageDetailList = tlStorageDetailMapper.selectByMap(map);
        return tlStorageDetailList;
    }

}
